﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.IO;
using System.Reflection;

namespace BinaryTimes
{
    class Screensaver
    {
        public bool Start(string[] args)
        {
            int? excludeScreen = null;

            if (args.Length > 0)
            {
                if (args[0].ToLower().StartsWith("/p"))
                {
                    return false;
                }
                else if (args[0].ToLower().StartsWith("/c"))
                {
                    string strParent = args[0].Substring(args[0].IndexOf(":") + 1, args[0].Length - args[0].IndexOf(":") - 1);

                    int iParent = 0;
                    if (Int32.TryParse(strParent, out iParent))
                    {
                        IntPtr parent = new IntPtr(iParent);
                        System.Windows.Forms.MessageBox.Show(new Win32Window(parent), "No settings available.", "Binary Times", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    return false;
                }
                else if (args[0].ToLower().StartsWith("excludescreen:"))
                {
                    string screen = args[0].Substring(args[0].IndexOf(":") + 1, args[0].Length - args[0].IndexOf(":") - 1);

                    int tmp;
                    if (Int32.TryParse(screen, out tmp))
                        excludeScreen = tmp;
                }
            }

            Start(excludeScreen);
            return true;
        }

        private void Start(int? excludeScreen)
        {
            Cursor.Hide();

            MediaPlayer mediaPlayer = null;
            string initFile = string.Format(@"{0}\init.mp3", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            bool deviceRecognition = File.Exists(string.Format(@"{0}\DeviceRecognition", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));

            if (File.Exists(initFile))
            {
                mediaPlayer = new MediaPlayer();
                mediaPlayer.Open(new Uri(initFile, UriKind.Relative));
            }
            
            for (int i = 0; i < Screen.AllScreens.Length; i++)
            {
                if (excludeScreen.HasValue && excludeScreen.Value == i)
                    continue;

                Screen screen = Screen.AllScreens[i];

                MainWindow window = new MainWindow(deviceRecognition);

                window.Width = screen.Bounds.Width;
                window.Height = screen.Bounds.Height;
                window.Left = screen.Bounds.Left;
                window.Top = screen.Bounds.Top;

                window.Closed += new EventHandler(window_Closed);
                window.Show();
            }

            if (mediaPlayer != null)
                mediaPlayer.Play();
        }

        void window_Closed(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }
    }
}
